package com.vanky.graph.service.impl;

import com.vanky.graph.pojo.Vertex;
import com.vanky.graph.properties.HugeClientProperties;
import com.vanky.graph.service.VertexService;
import com.vanky.graph.utils.JsonUtil;
import groovy.util.logging.Slf4j;
import org.apache.hugegraph.driver.GremlinManager;
import org.apache.hugegraph.driver.HugeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Slf4j
public class VertexServiceImpl implements VertexService {

    @Autowired
    HugeClientProperties hugeClientProperties;

    /*
     * 查询所有节点
     */
    @Override
    public List<Vertex> getAllVertex(){

        //连接到 HugeGraph 数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(),hugeClientProperties.getGraph())
                .build();

        //获取 gremlin 管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().limit(300)").execute().data();//TODO 这里修改了节点查询的语句

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, Vertex.class);
    }

    /*
     * 根据id查询所有节点
     */
    @Override
    public List<Vertex> getVertexById(String id){

        //连接到 HugeGraph 数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(),hugeClientProperties.getGraph())
                .build();

        //获取 gremlin 管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().hasId('"+id+"').both().dedup()").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, Vertex.class);
    }

}